﻿using MesBSW.Tools;
using Sunny.UI;
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using ZXing;
using ZXing.Common;

namespace Gl_mes.View
{
    public partial class TestFrm : UIPage
    {
        BarCodeHook BarCode = new BarCodeHook();

        public TestFrm()
        {
            InitializeComponent();
            BarCode.BarCodeEvent += new BarCodeHook.BarCodeDelegate(BarCode_BarCodeEvent);
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            BarCode.Start();
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            BarCode.Stop();
        }

        private delegate void ShowInfoDelegate(BarCodeHook.BarCodes barCode);

        private void HandleBarCode(BarCodeHook.BarCodes barCode)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new ShowInfoDelegate(HandleBarCode), new object[] { barCode });
            }
            else
            {
                if (barCode.IsValid)
                {
                    //using (MesDB ctx = new MesDB())
                    //{
                    //    StatusType tmpBean = new StatusType();
                    //    tmpBean.StypeCode = "AAA";
                    //    tmpBean.StypeName = "BBB";
                    //    ctx.StatusTypes.Add(tmpBean);
                    //    ctx.SaveChanges();
                    //}
                }
            }
        }

        private void BarCode_BarCodeEvent(BarCodeHook.BarCodes barCode)
        {
            HandleBarCode(barCode);
        }

        int qr_x = 0;
        int qr_y = 0;

        private void InitSpectPrintDoc(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Graphics g = e.Graphics;
            BarcodeWriter bcWriter = new BarcodeWriter();
            bcWriter.Format = BarcodeFormat.QR_CODE;
            var options = new EncodingOptions
            {
                Height = 70,
                Width = 70,
                Margin = 0
            };
            bcWriter.Options = options;
            Bitmap bcImage = bcWriter.Write(this.textBox1.Text);
            int x = 10;
            int y = 10;
            g.DrawImage(bcImage, x, y);

            using (Font font = new Font("Arial", 7, FontStyle.Regular))
            {
                g.DrawString(this.textBox2.Text, font, Brushes.Black, new PointF(x, y + 70 + 5));
            }
        }

        private void uiButton3_Click(object sender, EventArgs e)
        {

            PrintDocument pd = new PrintDocument();
            pd.DefaultPageSettings.Landscape = false;
            pd.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
            pd.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("aaa", 236, 118);
            pd.PrintPage += new PrintPageEventHandler(this.InitSpectPrintDoc);
           

            PrintPreviewDialog tmpDia = new PrintPreviewDialog();
            tmpDia.Document = pd;
            if (tmpDia.ShowDialog() == DialogResult.OK)
            {
                pd.Print();
            }
        }
    }
}
